package com.project.controller;

import com.project.bean.SellCarBean;
import com.project.bean.SellOrderBean;
import com.project.bean.UserBean;
import com.project.service.ISellCarService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 销售车控制器
 */

@RestController
@RequestMapping("sellCar")
@RequiresPermissions("sell:am")
public class SellCarController {
    @Autowired
    private ISellCarService sellCarService;
     @RequestMapping("findSellcarByloginUser")
    public List<SellCarBean> findSellcarByloginUser(HttpServletRequest req){
         UserBean loginUser= (UserBean) req.getSession().getAttribute("loginUser");
         return sellCarService.findSellcarByloginUser(loginUser.getUserName());
     }

    /**
     * 判断该商品在销售车中是否存在，如果存在，则数量加一；如果不存在，则新增销售车商品
     * @param productId 商品ID
     */
    @RequestMapping("addSellCarShop")
    public String addSellCarShop(Integer productId,HttpServletRequest req){
        UserBean loginUser= (UserBean) req.getSession().getAttribute("loginUser");
       sellCarService.addSellCarShop(productId,loginUser.getUserName());
       return "ok";
    }

    /**
     * 根据销售车ID，删除销售车记录
     * @param sellCarId 销售车ID
     */
    @RequestMapping("delShop")
    public String delShop(Integer sellCarId){
        sellCarService.delShop(sellCarId);
        return "ok";
    }

    /**
     * 根据销售车ID，修改销售车商品数量
     * @param sellCarId 销售车ID
     * @param newNum 新数量
     */
    @RequestMapping("updateSellcarNum")
    public String updateSellcarNum(Integer sellCarId,Integer newNum){
        sellCarService.updateSellcarNum(sellCarId, newNum);
        return "ok";
    }

    /**
     * 销售商品出库
     * 1、添加销售基本信息
     *2、根据销售员(或销售员ID)查询销售车信息集合
     *3、遍历销售车集合，循环添加销售明细，循环修改库存数量（类减）
     *4、根据销售员id清空销售车
     * @param sellOrder 出库销售信息对象
     */
    @RequestMapping("removeStock")
    public String removeStock(SellOrderBean sellOrder, HttpServletRequest req){
        UserBean loginUser= (UserBean) req.getSession().getAttribute("loginUser");
        sellCarService.removeStock(sellOrder, loginUser.getUserName());
        return "ok";
    }
}
